import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpStatus,
} from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import * as path from 'path';
import { CommonConfig, Utils } from '../index';
const STATIC_PAGE_LIST = CommonConfig.STATIC_PAGE_LIST;
@Catch(HttpException)
export class BussinessExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();
    // const status = exception.getStatus();
    // 检查是否地址栏URL请求
    if (Utils.Url.checkUrlRule(STATIC_PAGE_LIST, request.url)) {
      response.sendFile(
        path.join(__dirname, '../../../', CommonConfig.STATIC_PATH),
      );
      return;
    }
    const exceptions = exception.getResponse();
    // console.log('bussiness filter', exceptions);
    response.status(HttpStatus.BAD_REQUEST).json({
      ode: exceptions['code'],
      message: exceptions['message'],
      data: {},
    });
  }
}
